package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopInfoAuditLoginReturnBO;
import com.ztgf.shopping.models.bo.shop.ShopInfoAuditOpenReturnBO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.shop.ShopBlacklistDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoAuditDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.service.shop.IShopBlacklistService;
import com.ztgf.shopping.models.service.shop.IShopInfoAuditService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.vo.shop.ShopInfoAuditLoginVO;
import com.ztgf.shopping.models.vo.shop.ShopInfoAuditOpenVO;
import com.ztgf.user.models.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 店铺审核表控制器
 *
 * @author zhanghualang
 * @Date 2020-06-23 11:56:32
 */

@RestController
@RequestMapping("/api/shopInfoAudit")
@Slf4j
@Api(value = "ShopInfoAuditApiController",tags = "店铺审核表Api接口")
public class ShopInfoAuditApiController {

    @DubboReference
    private IShopInfoAuditService shopInfoAuditService;

    @DubboReference
    private IShopBlacklistService iShopBlacklistService;

    @DubboReference
    private IUserLoginService userLoginService;

    @DubboReference
    private IShopInfoService iShopInfoService;

    @DubboReference
    private IAuditService iAuditService;

    @PostMapping("/loginReturn")
    @ApiOperation(value = "买家重新提交资料",notes = "买家重新提交资料")
    public Result loginReturn(ShopInfoAuditLoginReturnBO returnBO){
        //验证是否已经被加入黑名单
        //黑名单类型 0用户黑名单，1店铺黑名单
        ShopBlacklistDTO shopBlacklistDTO = iShopBlacklistService.getByCompanyName(returnBO.getName(), ShoppingCodeEnum.BLACKLIST_LOGIN.getCode());
        if (shopBlacklistDTO != null){
            return new Result<>(CodeEnum.FAIL_AddUser_3006);
        }

        boolean flag = shopInfoAuditService.loginReturn(returnBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/getLogin")
    @ApiOperation(value = "根据id查找注册详情",notes = "根据id查找注册详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditLoginVO> getLogin(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", id).eq("type", ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode());
        ShopInfoAuditDTO shopInfoAuditDTO = shopInfoAuditService.selectOne(wrapper);
        ShopInfoAuditLoginVO vo = shopInfoAuditService.getLogin(shopInfoAuditDTO.getId());
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getOpen")
    @ApiOperation(value = "根据店铺ID查找开店详情",notes = "根据店铺ID查找开店详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditOpenVO> getOpen(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", id).eq("type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());
        ShopInfoAuditDTO shopInfoAuditDTO = shopInfoAuditService.selectOne(wrapper);
        ShopInfoAuditOpenVO vo = shopInfoAuditService.getOpen(shopInfoAuditDTO.getId());
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/openReturn")
    @ApiOperation(value = "卖家重新提交资料",notes = "卖家重新提交资料")
    public Result openReturn(ShopInfoAuditOpenReturnBO returnBO){
        if (StringUtils.isEmpty(returnBO.getShopName()) || StringUtils.isEmpty(returnBO.getDomain())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //验证是否已经被加入黑名单
        //黑名单类型 0用户黑名单，1店铺黑名单
        ShopBlacklistDTO shopBlacklistDTO = iShopBlacklistService.getByShopId(returnBO.getShopId(), ShoppingCodeEnum.BLACKLIST_LOGIN.getCode());
        if (shopBlacklistDTO != null){
            return new Result<>(CodeEnum.FAIL_AddUser_3006);
        }

        //验证店铺名重复
        if (iShopInfoService.getbyShopName(returnBO.getShopName())){
            return new Result<>(CodeEnum.FAIL_AddUser_3008);
        }
        //验证二级域名重复
        if (iShopInfoService.getbyDomain(returnBO.getDomain())){
            return new Result<>(CodeEnum.FAIL_AddUser_3009);
        }

        boolean flag = shopInfoAuditService.openReturn(returnBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/getProcessLogin")
    @ApiOperation(value = "根据userid查找注册流程详情",notes = "根据userid查找注册流程详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<AuditDTO> getProcessLogin(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Wrapper<AuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", id).eq("type", ShoppingCodeEnum.AUDIT_TYPE_LOGIN.getCode());
        AuditDTO auditDTO = iAuditService.selectOne(wrapper);
        if (auditDTO == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(auditDTO);
        }
    }
}
